<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Order extends Model
{
    //
    use SoftDeletes;
    protected $dates = ['deleted_at'];
    protected $table = 'orders';

    protected $fillable = [
        'user_id', 'order_number', 'total_amount', 'status', 'bistro_id', 'position_id', 'date', 'sort_number', 'order_status', 'remarks'
    ];

    public function scopUser($query, $user_id) {
        if(!empty($user_id)) {
            return $query->where('user_id', $user_id);
        }
        return $query;
    }

    public function scopeSortNumber($query, $sort_number) {
        if(!empty($sort_number)) {
            return $query->where('sort_number', $sort_number);
        }
        return $query;
    }

    public function scopeOrderNumber($query, $order_number) {
        if(!empty($order_number)) {
            return $query->where('order_number', 'like', '%'.$order_number.'%');
        }
        return $query;
    }

    public function scopeTotalAmount($query, $start, $end) {
        if(!empty($start) && !empty($end)) {
            $query->where('total_amount', '>=', $start)->where('total_amount', '<=', $end);
        }
        if(!empty($start)) {
            $query->where('total_amount', '>=', $start);
        }
        if(!empty($end)) {
            $query->where('total_amount', '<=', $end);
        }
        return $query;
    }


    public function scopeStatus($query, $status) {
        if(!empty($status)) {
            return $query->whereIn('status', $status);
        }
        return $query;
    }

    public function scopeBistroId($query, $bistro_id) {
        if(!empty($bistro_id)) {
            return $query->where('bistro_id', $bistro_id);
        }
        return $query;
    }

    public function scopeDate($query, $date) {
        if(!empty($date)) {
            return $query->where('date', $date);
        }
        return $query;
    }
}
